﻿#include "ExpendedImage.h"
#include <QGraphicsItem>
#include <QDebug>
#include <QMenu>
#include <QScrollBar>
#include <qpushbutton.h>
#include <qradiobutton.h>

ExpendedImage::ExpendedImage(QWidget* parent)
	: QWidget(parent)
	, ui(new Ui::ExpendedImageClass())
{
	ui->setupUi(this);
	ItemType = ELLIPSE_ITEM;
	p_RiggerScene = new RiggerScene(ui->graphicsView->size(), ItemType);
	ui->graphicsView->setScene(p_RiggerScene);
	ui->graphicsView->setViewportUpdateMode(QGraphicsView::FullViewportUpdate);
}

ExpendedImage::~ExpendedImage()
{
	delete ui;
}
void ExpendedImage::mousePressEvent(QMouseEvent* event)
{
	QPoint pos = event->pos();
	QWidget::mousePressEvent(event);
}

void ExpendedImage::resizeEvent(QResizeEvent* event)
{
	QWidget::resizeEvent(event);
}

void ExpendedImage::showEvent(QShowEvent* event)
{
	static bool firstflag = true;
	if (firstflag)
	{
		p_RiggerScene->resizeScene(QRectF(QPoint(0, 0), ui->graphicsView->size()));
		firstflag = false;
	}
}


void ExpendedImage::on_graphicsView_customContextMenuRequested(const QPoint& pos)
{
	QMenu menu;
	QAction* action = menu.addAction(QIcon(":/icons/add_plus.png"), QString("添加图形"));
	if (menu.exec(QCursor::pos()) == action)
	{
		QPen pen(Qt::red, 1, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin);
		switch (ItemType) {
		case ELLIPSE_ITEM:
		{
			QGraphicsEllipseItem* ellipse = new QGraphicsEllipseItem(QRect(pos, QSize(200, 200)));
			ellipse->setAcceptHoverEvents(true);
			ellipse->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
			pen.setWidth(1);
			ellipse->setPen(pen);
			ellipse->setBrush(QBrush(Qt::red));
			ellipse->setOpacity(0.8);
			p_RiggerScene->addItem(ellipse);
		}
		break;
		case LINE_ITEM:
		{
			QGraphicsLineItem* line = new QGraphicsLineItem(QLine(pos, pos + QPoint(100, 50)));
			line->setAcceptHoverEvents(true);
			line->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
			pen.setWidth(3);
			line->setPen(pen);
			line->setOpacity(0.8);
			p_RiggerScene->addItem(line);
		}
		break;
		case RECT_ITEM:
		{
			QGraphicsRectItem* rect = new QGraphicsRectItem(QRect(pos, QSize(200, 150)));
			rect->setAcceptHoverEvents(true);
			rect->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
			pen.setWidth(1);
			rect->setPen(pen);
			rect->setBrush(QBrush(Qt::red));
			rect->setOpacity(0.8);
			p_RiggerScene->addItem(rect);
		}
		break;
		case IMAGE_ITEM:
		{
			QGraphicsPixmapItem* pixmap = new QGraphicsPixmapItem(QPixmap(":/icons/circle_green.png"));
			pixmap->setAcceptHoverEvents(true);
			pixmap->setOffset(pos);
			pixmap->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
			p_RiggerScene->addItem(pixmap);
		}
		break;
		case ANCHOR_ITEM:
		{
			AnchorItem* item = new AnchorItem(QRect(pos, QSize(200, 200)));
			p_RiggerScene->addItem(item);
		}
		break;
		default:
			break;
		}
	}
}

